﻿using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Model.ModelsExt;
using AntUnion.FrameWork.Model;
using AntUnion.FrameWork.Util;
using AntUnion.FrameWork.Web.Common;
using AntUnion.FrameWork.Web.Service;
using MetaQuotes.MT5WebAPI.Common;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using System.Linq;

namespace AntUnion.FrameWork.Web.Controllers
{
    public class WithdrawController : BaseController
    {
        #region 出金

        /// <summary>
        /// 提交出金申请
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetCurrency()
        {
            Result res = new Result();
            try
            {
                List<string> list = assetsServer.GetCurrency();
                res.Code = 0;
                res.Data = list;
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
            }
            return new JsonResult(res);
        }


        /// <summary>
        /// 出金
        /// </summary>
        /// <returns></returns>
        public IActionResult Withdraw(string language = "user-zh-CN")
        {
            string defaultLanguage = "zh-CN";
            if (language == "user-en-US")
            {
                defaultLanguage = "en-US";
            }
            CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
            CrmUserInfo crmUserInfo = crmService.GetFinancial(crmUserModel.Id);
            ViewBag.noFinancial = true;
            if (crmUserInfo != null)
            {
                ViewBag.noFinancial = false;
            }
            CrmUser crmUser = crmService.GetCrmUser(crmUserModel.Id);
            ViewBag.noTradePassword = string.IsNullOrEmpty(crmUser.TradePwd);
            Mt5User mt5User = mt5Service.GetMt5UserById(crmUserModel.Id);
            ViewBag.hasMt5User = true;
            if (mt5User != null)
            {
                //更新结余
                MTAccount mTAccount = new MT5CommonHelper().QueryUserAccount((ulong)mt5User.Login);
                mt5Service.UpdateMt5Balance(mt5User.Login, mTAccount.Balance);
                ViewBag.hasMt5User = false;
                ViewBag.login = mt5User.Login;
                double balance = assetsServer.GetRealBalance(mTAccount);
                ViewBag.mt5Balance = balance.ToString("#,##0.00");
            }
            List<PublicService.SystemConfigModel> systemConfigModels = publicService.GetSystemConfig();
            ViewBag.withdrawalWarmPrompt = systemConfigModels.FirstOrDefault(k => k.name == "withdrawalWarmPrompt" && k.language == defaultLanguage).value;
            return View();
        }

        ///// <summary>
        ///// 提交出金申请
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //public JsonResult Withdraw(int channelId, int mothod, int mt5Id, string withdrawcurrency, string fromcurrency, decimal fees, decimal withdrawamount, decimal fromamount, string password, string remarks)
        //{
        //    Result res = new Result();
        //    try
        //    {
        //        CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
        //        object obj = assetsServer.GetCrmCollection(channelId, mothod);
        //        if (obj == null)
        //        {
        //            res.Code = 1;
        //            res.Msg = LanguageConfig.collectionAccontNull;
        //            return new JsonResult(res);
        //        }
        //        CrmUser crmUser = crmService.GetCrmUser(crmUserModel.Id);
        //        //确认用户是否存在
        //        if (crmUser == null)
        //        {
        //            res.Code = 1;
        //            res.Msg = LanguageConfig.crmUserNull;
        //            return new JsonResult(res);
        //        }
        //        if (string.IsNullOrEmpty(password))
        //        {
        //            res.Code = 1;
        //            res.Msg = LanguageConfig.treadPasswordNull;
        //            return new JsonResult(res);
        //        }
        //        //确认交易账户是否存在
        //        Mt5User mt5User = mt5Service.GetMt5UserById(mt5Id, crmUserModel.Id, (int)Mt5UserType.Real);
        //        if (mt5User == null)
        //        {
        //            res.Code = 1;
        //            res.Msg = LanguageConfig.mt5UserNull;
        //            return new JsonResult(res);
        //        }
        //        int withdrawalCount = assetsServer.GetWithdrawalCount(crmUserModel.Id);
        //        if(withdrawalCount >= 3)
        //        {
        //            res.Code = 1;
        //            res.Msg = LanguageConfig.tooMuchWithdrawal;
        //            return new JsonResult(res);
        //        }
        //        //核对实际支付金额和手续费
        //        RealAmount realAmount = assetsServer.Commission(fromcurrency, withdrawcurrency, withdrawamount);
        //        if (realAmount == null)
        //        {
        //            res.Code = 1;
        //            res.Msg = LanguageConfig.exchangeRateNull;
        //            return new JsonResult(res);
        //        }
        //        if (fromamount != realAmount.PayAmount)
        //        {
        //            res.Code = 1;
        //            res.Msg = LanguageConfig.amountInconsistent;
        //            return new JsonResult(res);
        //        }

        //        //确认密码是否正确
        //        string encryptPwd = SecurityUtil.EncryptPwd(password);
        //        if (crmUser.TradePwd != encryptPwd)
        //        {
        //            res.Code = 1;
        //            res.Msg = LanguageConfig.tradePwdError;
        //            return new JsonResult(res);
        //        }

        //        MT5CommonHelper mT5CommonHelper = new MT5CommonHelper();
        //        MTUser _mt5User = mT5CommonHelper.QueryUsers(Convert.ToUInt64(mt5User.Login));
        //        if ((double)realAmount.PayAmount > _mt5User.Balance)
        //        {
        //            res.Code = 1;
        //            res.Msg = LanguageConfig.insufficientBalance;
        //            return new JsonResult(res);
        //        }
        //        using (mtContext mtContext = new mtContext())
        //        {
        //            ulong ticker;
        //            bool MTRetCode = mT5CommonHelper.TradeBalance(mtContext, Convert.ToUInt64(mt5User.Login), MTDeal.EnDealAction.DEAL_BALANCE, "withdrawalOfFunds", 0 - Convert.ToDouble(realAmount.PayAmount), "", out ticker);
        //            if (MTRetCode)
        //            {
        //                CrmWithdrawal crmWithdrawal = new CrmWithdrawal()
        //                {
        //                    Id = CommonHelper.GetOrderNo(),
        //                    CrmId = crmUserModel.Id,
        //                    Login = mt5User.Login,
        //                    Mothod = mothod,
        //                    FromAmount = withdrawamount,
        //                    FromAmountCurrency = withdrawcurrency,
        //                    ToAmount = fromamount,
        //                    ToAmountCurrency = fromcurrency,
        //                    ExchangeRate = realAmount.Rate,
        //                    WithdrawalAmount = realAmount.PayAmount,
        //                    WithdrawalAmountCurrency = fromcurrency,
        //                    State = 0,
        //                    Fees = realAmount.Commission,
        //                    CreateDate = DateTime.Now,
        //                    Deducted = 1,
        //                    Account = obj.ToJson()
        //                };
        //                MTUser tmu = mT5CommonHelper.QueryUsers(Convert.ToUInt64(mt5User.Login));
        //                mt5Service.UpdateMt5Balance(mt5User.Login, tmu.Balance);
        //                int count = assetsServer.Withdrawal(ref crmWithdrawal);
        //                CrmAuditLog crmAuditLog = new CrmAuditLog()
        //                {
        //                    OrderId = crmWithdrawal.Id,
        //                    CrmId = crmUserModel.Id,
        //                    Type = 4,
        //                    Cont = "提交审核",
        //                    CreateDate = DateTime.Now
        //                };
        //                crmService.AddAuditLog(crmAuditLog);
        //                res.Code = 0;
        //                res.Msg = count > 0 ? LanguageConfig.submitSuccess : LanguageConfig.submitFail;
        //            }
        //            else
        //            {
        //                res.Code = 1;
        //                res.Msg = LanguageConfig.withdrawalitFail;
        //                return new JsonResult(res);
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        res.Code = 1;
        //        res.Msg = ex.Message;
        //    }
        //    return new JsonResult(res);
        // }

        /// <summary>
        /// 提交出金申请
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Withdraw(int channelId, int mothod, decimal withdrawamount, string password, string remarks)
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                object obj = assetsServer.GetCrmCollection(channelId, mothod);
                if (obj == null)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.collectionAccontNull;
                    return new JsonResult(res);
                }
                CrmUser crmUser = crmService.GetCrmUser(crmUserModel.Id);
                //确认用户是否存在
                if (crmUser == null)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.crmUserNull;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(password))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.treadPasswordNull;
                    return new JsonResult(res);
                }
                //确认交易账户是否存在
                Mt5User mt5User = mt5Service.GetMt5UserById(crmUserModel.Id);
                if (mt5User == null)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.mt5UserNull;
                    return new JsonResult(res);
                }
                int withdrawalCount = assetsServer.GetWithdrawalCount(crmUserModel.Id);
                if (withdrawalCount >= 3)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.tooMuchWithdrawal;
                    return new JsonResult(res);
                }
                if (withdrawamount <= 0)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.withdrawCanNotLessThan0;
                    return new JsonResult(res);
                }
                //确认密码是否正确
                string encryptPwd = SecurityUtil.EncryptPwd(password);
                if (crmUser.TradePwd != encryptPwd)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.tradePwdError;
                    return new JsonResult(res);
                }

                MT5CommonHelper mT5CommonHelper = new MT5CommonHelper();
                MTAccount mTAccount = new MT5CommonHelper().QueryUserAccount((ulong)mt5User.Login);
                double balance = (mTAccount.Balance > mTAccount.Equity ? mTAccount.Equity : mTAccount.Balance) - mTAccount.Margin;
                if (balance < 0)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.insufficientBalance;
                    return new JsonResult(res);
                }
                if ((double)withdrawamount > balance)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.insufficientBalance;
                    return new JsonResult(res);
                }
                using (mtContext mtContext = new mtContext())
                {
                    ulong ticker;
                    bool MTRetCode = mT5CommonHelper.TradeBalance(mtContext, Convert.ToUInt64(mt5User.Login), MTDeal.EnDealAction.DEAL_BALANCE, "withdrawalOfFunds", 0 - Convert.ToDouble(withdrawamount), "", 0 - Convert.ToDouble(withdrawamount), 0, crmUser.LoginName, out ticker);
                    if (MTRetCode)
                    {
                        CrmWithdrawal crmWithdrawal = new CrmWithdrawal()
                        {
                            Id = CommonHelper.GetOrderNo(),
                            CrmId = crmUserModel.Id,
                            Login = mt5User.Login,
                            Mothod = mothod,
                            FromAmount = withdrawamount,
                            FromAmountCurrency = "USD",
                            ToAmount = withdrawamount,
                            ToAmountCurrency = "USD",
                            ExchangeRate = 0,
                            WithdrawalAmount = withdrawamount,
                            WithdrawalAmountCurrency = "USD",
                            State = 0,
                            Fees = 0,
                            CreateDate = DateTime.Now,
                            Deducted = 1,
                            Account = obj.ToJson(),
                            Remark = remarks,
                            Mt5Order = ticker.ToString()
                        };
                        MTUser tmu = mT5CommonHelper.QueryUsers(Convert.ToUInt64(mt5User.Login));
                        mt5Service.UpdateMt5Balance(mt5User.Login, tmu.Balance);
                        int count = assetsServer.Withdrawal(ref crmWithdrawal);
                        CrmAuditLog crmAuditLog = new CrmAuditLog()
                        {
                            OrderId = crmWithdrawal.Id,
                            CrmId = crmUserModel.Id,
                            Type = 4,
                            Cont = "提交审核",
                            CreateDate = DateTime.Now
                        };
                        crmService.AddAuditLog(crmAuditLog);
                        res.Code = 0;
                        res.Msg = count > 0 ? LanguageConfig.submitSuccess : LanguageConfig.submitFail;
                    }
                    else
                    {
                        res.Code = 1;
                        res.Msg = LanguageConfig.withdrawalitFail;
                        return new JsonResult(res);
                    }
                }
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
            }
            return new JsonResult(res);
        }


        #endregion

        #region 电汇账户

        /// <summary>
        /// 电汇账户
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateWireTransfer()
        {
            CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
            CrmUser crmUser = crmService.GetCrmUser(crmUserModel.Id);
            ViewBag.noTradePassword = string.IsNullOrEmpty(crmUser.TradePwd);
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CreateWireTransfer(CrmWireTransfer model)
        {
            Result res = new Result();
            try
            {
                if (string.IsNullOrEmpty(model.Currency))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterCurrency;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Payee))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterPayee;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.AccountNumber))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterAccountNumber;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.PayeeAddress))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterPayeeaddress;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Bank))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterBank;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Swiftcode))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterSwiftcode;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.BankAddress))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterbankaddress;
                    return new JsonResult(res);
                }
                CrmWireTransfer crmWireTransfer = assetsServer.GetMyWireTransfer(model.AccountNumber);
                if (crmWireTransfer != null)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.hasCrmWireTransfer;
                    return new JsonResult(res);
                }
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                model.CrmId = crmUserModel.Id;
                int count = assetsServer.CreateWireTransfer(model);
                if (count > 0)
                {
                    res.Code = 0;
                    res.Msg = LanguageConfig.saveSuccess;
                }
                else
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.saveFailed;
                }
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 电汇账户
        /// </summary>
        /// <returns></returns>
        public IActionResult EditWireTransfer(int id)
        {
            CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
            CrmWireTransfer crmWireTransfer = assetsServer.GetCrmWireTransfers(id, crmUserModel.Id);
            return View(crmWireTransfer);
        }

        /// <summary>
        /// 电汇账户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult EditWireTransfer(CrmWireTransfer model)
        {
            Result res = new Result();
            try
            {
                if (string.IsNullOrEmpty(model.Currency))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterCurrency;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Payee))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterPayee;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.AccountNumber))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterAccountNumber;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.PayeeAddress))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterPayeeaddress;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Bank))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterBank;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Swiftcode))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterSwiftcode;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.BankAddress))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterbankaddress;
                    return new JsonResult(res);
                }
                //CrmWireTransfer crmWireTransfer = assetsServer.GetMyWireTransfer(model.AccountNumber);
                //if (crmWireTransfer != null)
                //{
                //    res.Code = 1;
                //    res.Msg = LanguageConfig.hasCrmWireTransfer;
                //    return new JsonResult(res);
                //}
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                model.CrmId = crmUserModel.Id;
                int count = assetsServer.EditWireTransfer(model);
                if (count > 0)
                {
                    res.Code = 0;
                    res.Msg = LanguageConfig.saveSuccess;
                }
                else
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.saveFailed;
                }
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 电汇账户
        /// </summary>
        /// <returns></returns>
        public IActionResult DeleteWireTransfer(int id)
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                int count = assetsServer.DelWireTransfer(id, crmUserModel.Id);
                if (count > 0)
                {
                    res.Code = 0;
                    res.Msg = LanguageConfig.deleteSuccess;
                    return new JsonResult(res);
                }
                else
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.deleteFailed;
                    return new JsonResult(res);
                }
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 获取电汇账户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult CrmWireTransfer()
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                List<CrmWireTransfer> wireTransferList = assetsServer.GetCrmWireTransfers(crmUserModel.Id);
                res.Code = 1;
                res.Data = wireTransferList;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }

        #endregion

        #region 汇款账户

        /// <summary>
        /// 汇款账户
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateRemittanceAccount()
        {
            CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
            if (crmUserModel == null)
            {
                return NotFound();
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CreateRemittanceAccount(CrmRemittanceAccount model)
        {
            Result res = new Result();
            try
            {
                if (string.IsNullOrEmpty(model.Currency))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterCurrency;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Payee))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterPayee;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.AccountNumber))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterAccountNumber;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Bank))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterBank;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.BranchName))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterBranchName;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Qrcode))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterQrcode;
                    return new JsonResult(res);
                }
                CrmRemittanceAccount crmRemittanceAccount = assetsServer.GetMyRemittanceAccount(model.AccountNumber);
                if (crmRemittanceAccount != null)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.hasCrmRemittanceAccount;
                    return new JsonResult(res);
                }
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                model.CrmId = crmUserModel.Id;
                int count = assetsServer.CreateRemittanceAccount(model);
                if (count > 0)
                {
                    res.Code = 0;
                    res.Msg = LanguageConfig.submitSuccess;
                }
                else
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.submitFail;
                }
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 汇款账户
        /// </summary>
        /// <returns></returns>
        public IActionResult EditRemittanceAccount(int id)
        {
            CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
            if (crmUserModel == null)
            {
                return NotFound();
            }
            CrmRemittanceAccount crmRemittanceAccount = assetsServer.GetCrmRemittanceAccount(id, crmUserModel.Id);
            return View(crmRemittanceAccount);
        }

        /// <summary>
        /// 电汇账户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult EditRemittanceAccount(CrmRemittanceAccount model)
        {
            Result res = new Result();
            try
            {
                if (string.IsNullOrEmpty(model.Currency))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterCurrency;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Payee))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterPayee;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.AccountNumber))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterAccountNumber;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Bank))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterBank;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.BranchName))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterBranchName;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Qrcode))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterQrcode;
                    return new JsonResult(res);
                }
                //CrmRemittanceAccount crmRemittanceAccount = assetsServer.GetMyRemittanceAccount(model.AccountNumber);
                //if (crmRemittanceAccount != null)
                //{
                //    res.Code = 1;
                //    res.Msg = LanguageConfig.hasCrmRemittanceAccount;
                //    return new JsonResult(res);
                //}
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                model.CrmId = crmUserModel.Id;
                int count = assetsServer.EditRemittanceAccount(model);
                if (count > 0)
                {
                    res.Code = 0;
                    res.Msg = LanguageConfig.submitSuccess;
                }
                else
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.submitFail;
                }
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 电汇账户
        /// </summary>
        /// <returns></returns>
        public IActionResult DeleteRemittanceAccount(int id)
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                int count = assetsServer.DelRemittanceAccount(id, crmUserModel.Id);
                if (count > 0)
                {
                    res.Code = 0;
                    res.Msg = LanguageConfig.deleteSuccess;
                    return new JsonResult(res);
                }
                else
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.deleteFailed;
                    return new JsonResult(res);
                }
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 获取汇款账户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult CrmRemittanceAccount()
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                List<CrmRemittanceAccount> remittanceAccountList = assetsServer.GetCrmRemittanceAccounts(crmUserModel.Id);
                res.Code = 1;
                res.Data = remittanceAccountList;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }

        #endregion

        #region 加密货币

        /// <summary>
        /// 汇款账户
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateCryptocurrency()
        {
            CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
            if (crmUserModel == null)
            {
                return NotFound();
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CreateCrmCryptocurrency(CrmCryptocurrency model)
        {
            Result res = new Result();
            try
            {
                if (string.IsNullOrEmpty(model.Currency))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterCurrency;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Network))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterNetwork;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.ReceivingAddress))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterReceivingAddress;
                    return new JsonResult(res);
                }
                CrmCryptocurrency crmCryptocurrency = assetsServer.GetMyCryptocurrency(model.ReceivingAddress);
                if (crmCryptocurrency != null)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.hasCrmCryptocurrency;
                    return new JsonResult(res);
                }
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                model.CrmId = crmUserModel.Id;
                int count = assetsServer.CreateCryptocurrency(model);
                if (count > 0)
                {
                    res.Code = 0;
                    res.Msg = LanguageConfig.saveSuccess;
                }
                else
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.saveFailed;
                }
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 汇款账户
        /// </summary>
        /// <returns></returns>
        public IActionResult EditCryptocurrency(int id)
        {
            CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
            if (crmUserModel == null)
            {
                return NotFound();
            }
            CrmCryptocurrency crmCryptocurrency = assetsServer.GetCrmCryptocurrencys(id, crmUserModel.Id);
            return View(crmCryptocurrency);
        }

        /// <summary>
        /// 电汇账户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult EditCrmCryptocurrency(CrmCryptocurrency model)
        {
            Result res = new Result();
            try
            {
                if (string.IsNullOrEmpty(model.Currency))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterCurrency;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.Network))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterNetwork;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(model.ReceivingAddress))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.enterReceivingAddress;
                    return new JsonResult(res);
                }
                //CrmCryptocurrency crmCryptocurrency = assetsServer.GetMyCryptocurrency(model.ReceivingAddress);
                //if (crmCryptocurrency != null)
                //{
                //    res.Code = 1;
                //    res.Msg = LanguageConfig.hasCrmCryptocurrency;
                //    return new JsonResult(res);
                //}
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                model.CrmId = crmUserModel.Id;
                int count = assetsServer.EditCryptocurrency(model);
                if (count > 0)
                {
                    res.Code = 0;
                    res.Msg = LanguageConfig.saveSuccess;
                }
                else
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.saveFailed;
                }
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 电汇账户
        /// </summary>
        /// <returns></returns>
        public IActionResult DeleteCrmCryptocurrency(int id)
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                int count = assetsServer.DelCryptocurrency(id, crmUserModel.Id);
                if (count > 0)
                {
                    res.Code = 0;
                    res.Msg = LanguageConfig.deleteSuccess;
                    return new JsonResult(res);
                }
                else
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.deleteFailed;
                    return new JsonResult(res);
                }
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 获取加密货币
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult CrmCryptocurrency()
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                List<CrmCryptocurrency> cryptoCurrencyList = assetsServer.GetCrmCryptocurrencys(crmUserModel.Id);
                res.Code = 1;
                res.Data = cryptoCurrencyList;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }

        #endregion
    }
}
